//
//  SettingsViewController.m
//  iOcr
//
//  Created by Matri Ning on 1/12/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "SettingsViewController.h"
#import "FunOCRAppDelegate.h"

@implementation SettingsViewController
@synthesize usernameInput,passwdInput;

@synthesize bbiSave;


- (void)dealloc {
    [super dealloc];
	
	[bbiSave release];
	[usernameInput release];
	[passwdInput release];

}






// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	appDelegate = (FunOCRAppDelegate *)[[UIApplication sharedApplication] delegate];			
	
	PlistHandle *plistHandler = [[PlistHandle alloc] initWithName:@"setting.plist"];
	
	[plistHandler writeContent:appDelegate.setting];
	
	appDelegate.setting = [plistHandler readContent];
	
	if (appDelegate.setting == nil) {
		NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"", @"username", @"", @"password", nil];
		
		appDelegate.setting = dic;
		
		NSLog(@"appDelegate.setting:: %@",appDelegate.setting);
	}
	
	NSLog(@"appDelegate.setting :: %d", ![[appDelegate.setting objectForKey:@"password"] isEqualToString:@""]);
	[plistHandler release];
	
	
	if ([appDelegate.setting objectForKey:@"password"] != nil && ![[appDelegate.setting objectForKey:@"password"] isEqualToString:@""]) {
		usernameInput.text = [appDelegate.setting objectForKey:@"username"];
		passwdInput.text = [appDelegate.setting objectForKey:@"password"];
		
	}
	
	usernameInput.borderStyle = UITextBorderStyleBezel;
	
	passwdInput.borderStyle = UITextBorderStyleBezel;
	
}





- (void)viewWillAppear:(BOOL)animated{

	[super viewWillAppear:animated];
	
	
	NSLog(@"appDelegate.setting at viewWillAppear :: %@", appDelegate.setting);
	
	

	if ([appDelegate.setting objectForKey:@"username"] != nil) {
		usernameInput.text = [appDelegate.setting objectForKey:@"username"];
	}
	else {
		[usernameInput becomeFirstResponder];
	}
	
	
	NSLog(@"[appDelegate.setting objectForKey :: %@", [appDelegate.setting objectForKey:@"password"]);
	passwdInput.text = [appDelegate.setting objectForKey:@"password"];

}







- (IBAction) saveSetting:(id) sender{
	
	bbiSave.enabled = NO;
	
	[appDelegate.setting setObject:usernameInput.text forKey:@"username"];
	
	[appDelegate.setting setObject:passwdInput.text forKey:@"password"];
	
	GDataServiceGoogleDocs *docsService = [appDelegate docsService];
	GDataServiceTicket *ticket;
	ticket = [docsService authenticateWithDelegate:self didAuthenticateSelector:@selector(docTicket:authenticatedWithError:)];
	
	
}






- (void)docTicket:(GDataServiceTicket *)ticket authenticatedWithError:(NSError *)error {
	
	PlistHandle *plistHandler;
	
	if (error == nil) {
		
		plistHandler = [[PlistHandle alloc]initWithName:@"setting.plist"];
		NSMutableDictionary *givenDict=[[NSMutableDictionary alloc] init];
		
		NSString *username = usernameInput.text;
		
		if ([username rangeOfString:@"@"].location == NSNotFound) {
			// if no domain was supplied, add @gmail.com
			username = [username stringByAppendingString:@"@gmail.com"];
		}
		
		
		
		[givenDict setObject:username forKey:@"username"];
		[givenDict setObject:passwdInput.text forKey:@"password"];
		[plistHandler writeContent:givenDict];
		
		[givenDict release];
		
		
		
		appDelegate.setting = [plistHandler readContent];
		
		NSLog( @"plist is %@", [appDelegate.setting objectForKey:@"username"]);
		
		
		
		
		[usernameInput resignFirstResponder];
		
		[passwdInput resignFirstResponder];
		
		UIAlertView *alertTest = [[UIAlertView alloc]
								  initWithTitle:@"Details Saved"
								  message:@"Details have been saved locally"
								  delegate:self
								  cancelButtonTitle:@"Back to Home"
								  otherButtonTitles:nil];
		
		[alertTest show];
		[alertTest autorelease];
		
	} else {
		
		
		plistHandler = [[PlistHandle alloc]initWithName:@"setting.plist"];
		
		appDelegate.setting = [plistHandler readContent];
		
		
		NSLog(@"appDelegate.setting :: %@", appDelegate.setting);
		
		UIAlertView *alertTest = [[UIAlertView alloc]
								  initWithTitle:@"Authetication failed"
								  message:@"Please check your username and password"
								  delegate:nil
								  cancelButtonTitle:@"Okay"
								  otherButtonTitles:nil];
		
		[alertTest show];
		[alertTest autorelease];
	}
	
	[plistHandler release];	
	
	bbiSave.enabled = YES;
}




- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
	self.tabBarController.selectedIndex = 0;
}




- (IBAction)backClicked:(id)sender{
	
	
	self.tabBarController.selectedIndex = 0;
}





- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}




- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}



@end
